package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import trip.HibernateUtil;
import trip.Route;
import trip.Route_stops;
import trip.Stop;

public class Route_stopsDAOImpl extends GenericDaoImpl<Route_stops> implements Route_stopsDAO {
	public Route_stopsDAOImpl(Class<Route_stops> type) {
		super(type);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Route_stops> getByRoute(Route route) {
		Session session = null;
		List<Route_stops> result = new ArrayList<Route_stops>();
		if (route == null)
			return result;
		try {
			session = HibernateUtil.getFactory().openSession();
			session.beginTransaction();
			Query query = session.createQuery("select rs from Route_stops rs "
					+ "where rs.route = :id order by rs.stop_number asc");
			query.setLong("id", route.getId());
			result = query.list();
			session.getTransaction().commit();
			return result;
		} finally {
			if (session != null && session.isOpen())
				session.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Route_stops> getByStop(Stop stop) {
		Session session = null;
		List<Route_stops> result = new ArrayList<Route_stops>();
		if (stop == null)
			return result;
		try {
			session = HibernateUtil.getFactory().openSession();
			session.beginTransaction();
			Query query = session.createQuery("select rs from Route_stops rs "
					+ "where rs.stop = :id order by rs.stop_number asc");
			query.setLong("id", stop.getId());
			result = query.list();
			session.getTransaction().commit();
			return result;
		} finally {
			if (session != null && session.isOpen())
				session.close();
		}
	}
}
